<style rel="stylesheet/scss" lang="scss" scoped>
@import "../../../styles/prod.scss";
.fl {
  float: left;
  color: red;
  font-size: 14px;
}
</style>
<template>
  <div class="app-container calendar-list-container">
    <img-list
      v-model="imgData.dialogVisible"
      :img-list="imgData.imgArr"
      :img-index="imgData.imgIndex"
    ></img-list>
    <div class v-for="(item,index) in ['产品型号','产品衍生型号']" :key="item">
      <div class="pds-tip">
        <div class="title">{{item}}</div>
      </div>
      <div class>
        <el-button
          class="filter-item pds-success"
          type="success"
          icon="search"
          @click="$router.push({path:'archives/model/add'})"
          v-if="index==0"
        >新增</el-button>
        <el-button
          class="filter-item pds-success"
          type="success"
          icon="search"
          @click="$router.push({path:'archives/model/addTrans'})"
          v-if="index==1"
        >新增</el-button>
        <el-button type="warning" @click="exportExcel()" v-if="index==0">下载模版</el-button>
        <div class="filter-container" style="float:right">
          <el-input
            style="width: 200px;"
            class="filter-item"
            placeholder="请输入机型号"
            v-model="pageData.params.plmModelNo"
            v-if="index==0"
          ></el-input>
          <el-input
            style="width: 200px;"
            class="filter-item"
            placeholder="请输入机型号"
            v-model="pageData2.params.plmModelNo"
            v-if="index==1"
          ></el-input>
          <el-button class="filter-item" type="primary" icon="search" @click="seach(index)">查询</el-button>
          <div class="filter-item" type="danger" style="margin-bottom:0">
            <el-upload
              :show-file-list="false"
              :action="importUrl"
              :headers="headers"
              :on-success="importSuccess"
            >
              <el-button
                size="small"
                class="filter-item"
                style="padding:11px 15px ;"
                type="warning"
              >导入</el-button>
            </el-upload>
          </div>
        </div>
      </div>
      <div class v-if="index==0">
        <el-table border fit highlight-current-row style="width: 100%" :data="pageData.list">
          <el-table-column width align="center" label="选择" v-if="index==0">
            <template slot-scope="scope">
              <el-radio
                v-model="pageData2.params.parentId"
                :label="scope.row.id"
                @change="choose"
              >选择</el-radio>
            </template>
          </el-table-column>
          <el-table-column width align="center" label="机型号" prop="plmModelNo"></el-table-column>
          <el-table-column width align="center" label="产品名称" prop="name"></el-table-column>
          <el-table-column width align="center" label="产品图片">
            <template slot-scope="scope">
              <el-button
                class
                type="success"
                size="mini"
                @click="previewImg(scope.row.pdsFileModelPbsPics)"
              >查看</el-button>
            </template>
          </el-table-column>
          <el-table-column width align="center" label="市场定位" prop="marketPositionDv"></el-table-column>
          <el-table-column width align="center" label="业务模式" prop="bizModelDv"></el-table-column>
          <el-table-column width align="center" label="上市年份" prop="salesYear"></el-table-column>
          <el-table-column width align="center" label="产品状态" prop="productStatusDv"></el-table-column>
          <el-table-column width align="center" label="编辑状态">
            <template slot-scope="scope">
              <span v-if="scope.row.status===0">草稿</span>
              <span v-if="scope.row.status===1">审核中</span> 
              <span v-if="scope.row.status===2">已发布</span>
              <span v-if="scope.row.status===3">已删除</span>
              <span v-if="scope.row.status===4">驳回</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="250px" fixed="right">
            <template slot-scope="scope">
              <el-button-group>
                <el-button
                  size="small"
                  type="success"
                  @click="$router.push({path:'list/model/detail',query:{modelPbsId:scope.row.id}})"
                >查看</el-button>
                <el-button
                  size="small"
                  type="primary"
                  @click="handleUpdate(null,scope.row.id)"
                  v-if="scope.row.status==0 || scope.row.status==4"
                >编辑</el-button>
                <el-button
                  size="small"
                  type="primary"
                  @click="handleChange(scope.row.id)"
                  v-if="scope.row.status==2"
                >变更</el-button>
                <el-button
                  size="small"
                  type="danger"
                  v-if="scope.row.status===0 || scope.row.status===4"
                  @click="handleDelete(scope.row)"
                >删除</el-button>
                <el-button
                  size="small"
                  type="info"
                  @click="$router.push({path:'archives/model/file',query:{modelPbsId:scope.row.id}})"
                >文档</el-button>
                <el-button
                  size="small"
                  type="warning"
                  v-if="scope.row.status===2"
                  @click="$router.push({path:'list/model/detail',query:{modelPbsId:scope.row.id,type:'export'}})"
                >导出</el-button>
              </el-button-group>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-container pds-pagination">
          <span class="fl">勾选主型号，下方表格中自动带出其衍生型号，只有已发布状态才能勾选</span>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handlePageChange"
            :current-page.sync="pageData.params.page"
            :page-sizes="[2,5,10,20]"
            :page-size="pageData.params.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageData.total"
            class="pds-pagination"
          ></el-pagination>
        </div>
      </div>
      <div class v-if="index==1">
        <el-table border fit highlight-current-row style="width: 100%" :data="pageData2.list">
          <el-table-column width align="center" label="机型号" prop="plmModelNo"></el-table-column>
          <el-table-column width align="center" label="产品名称" prop="name"></el-table-column>
          <el-table-column width align="center" label="产品图片">
            <template slot-scope="scope">
              <el-button
                class
                type="success"
                size="mini"
                @click="previewImg(scope.row.pdsFileModelPbsPics)"
              >查看</el-button>
            </template>
          </el-table-column>
          <el-table-column width align="center" label="市场定位" prop="marketPositionDv"></el-table-column>
          <el-table-column width align="center" label="业务模式" prop="bizModelDv"></el-table-column>
          <el-table-column width align="center" label="上市年份" prop="salesYear"></el-table-column>
          <el-table-column width align="center" label="产品状态" prop="productStatusDv"></el-table-column>
          <el-table-column width align="center" label="编辑状态">
            <template slot-scope="scope">
              <span v-if="scope.row.status===0">草稿</span>
              <span v-if="scope.row.status===1">审核中</span>
              <span v-if="scope.row.status===2">已发布</span>
              <span v-if="scope.row.status===3">已删除</span>
              <span v-if="scope.row.status===4">驳回</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="250px" fixed="right">
            <template slot-scope="scope">
              <el-button-group>
                <el-button
                  size="small"
                  type="success"
                  @click="$router.push({path:'list/model/detail',query:{modelPbsId:scope.row.id}})"
                >查看</el-button>
                <el-button
                  size="small"
                  type="primary"
                  @click="handleUpdate('trans',scope.row.id)"
                  v-if="scope.row.status==0 || scope.row.status==4"
                >编辑</el-button>
                <el-button
                  size="small"
                  type="primary"
                  @click="handleChange(scope.row.id)"
                  v-if="scope.row.status==2"
                >变更</el-button>
                <el-button
                  size="small"
                  type="danger"
                  v-if="scope.row.status===0 || scope.row.status==4"
                  @click="handleDelete(scope.row)"
                >删除</el-button>
                <el-button size="small" type="info" @click="$router.push({path:'archives/model/file',query:{modelPbsId:scope.row.id}})">文档</el-button>
                <el-button size="small" type="warning" v-if="scope.row.status===2" 
                @click="$router.push({path:'list/model/detail',query:{modelPbsId:scope.row.id,type:'export'}})">导出</el-button>
              </el-button-group>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-container pds-pagination">
          <el-pagination
            @size-change="handleSizeChange2"
            :current-page.sync="pageData2.params.page"
            :page-sizes="[2,5,10,20]"
            :page-size="pageData2.params.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageData2.total"
            class="pds-pagination"
          ></el-pagination>
        </div>
      </div>
    </div>
    <el-dialog title="导出模版" :visible.sync="ipVisible" width="30%">
      <el-cascader
        :options="basicForm[0].options"
        :props="{value:'id'}"
        v-model="pdsFileModelBaseDto.pdsConfCategory"
      ></el-cascader>
      <el-select v-model="pdsFileModelBaseDto.standard">
        <el-option
          v-for="item in [{labelEnUs:'EnStandard',labelZhCh:'欧标',value:1},{labelEnUs:'UsStandard',labelZhCh:'美标',value:2}]"
          :key="'standard' + item.value"
          :label="language=='en'?item.labelEnUs:item.labelZhCh"
          :value="item.value"
        ></el-option>
      </el-select>

      <span slot="footer" class="dialog-footer">
        <el-button @click="ipVisible = false">取 消</el-button>
        <el-button type="primary" @click="exportSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, removePlan } from "vuex";
import { getToken } from "@/utils/auth";
import * as archivesApi from "@/api/product/archives/index";
import * as prodApi from "@/api/product/index";
import { Message } from 'element-ui'
export default {
  name: "user",
  components: {
    // 'depart-selector': () =>
    //   import ('./components/departSelector')
  },
  data() {
    return {
      importUrl: process.env.BASE_API + "/api/pds/pdsFileExcel/importData",
      headers: {
        "X-Litemall-Admin-Token": getToken()
      },
      imgData: {
        dialogVisible: false,
        imgArr: [],
        imgIndex: 0
      },
      // 型号分页数据
      pageData: {
        listLoading: false,
        params: {
          page: 1,
          size: 5,
          variation: 0
        }
      },
      // 衍生型号分页数据
      pageData2: {
        listLoading: false,
        params: {
          page: 1,
          size: 5,
          parentId: 0, // 绑定主型号id-radio
          variation: 1
        }
      },
      pdsFileModelBaseDto: {},
      ipVisible: false,
      basicForm: [
        {
          label: "产品小类",
          unit: "",
          element: "cascader",
          type: "",
          name: "pdsConfCategory",
          options: [],
          props: {
            value: "id",
            label: this.language == "en" ? "nameEn" : "nameCn"
          },
          hidden: true,
          isRequired: true,
          rule: { required: true, type: "Array" }
        }
      ]
    };
  },
  created() {
    this.getDefaultOptions();
    // this.getList()
  },
  activated() {
    this.getList();
  },
  computed: {
    ...mapGetters(["elements", "language"])
  },
  watch: {
    // 'pdsFileModelBaseDto.pdsConfCategory'(val) {
    //   if (val.length === 3) {
    //     this.$refs.prodfile.prodCategoryChanged(val[2]);
    //   }
    // }
  },
  methods: {
    importSuccess(response, file, fileList) {
      console.log(response);

      if (response.code === 200) {
        Message({
          message: "导入成功",
          type: "success",
          duration: 5 * 1000
        });
      } else {
        Message({
            message: response.message,
            type: "error",
            duration: 5 * 1000
          })
      }
    },
    handleDelete(row) {
      console.log(row);
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        type: "warning"
      }).then(() => {
        console.log(row.id);
        const params = {
          id: row.id
        };
        archivesApi.removePlan(params).then(response => {
          console.log(response);
          if (response.code === 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getList();
            this.choose()
          }
        });
      });
    },
    getDefaultOptions() {
      this.getPdsConfCategoryTree();
    },
    getPdsConfCategoryTree() {
      prodApi.getPdsConfCategoryTree().then(res => {
        this.basicForm[0].options = res.data;
      });
    },
    previewImg(imgArr, index) {
      this.imgData.dialogVisible = true;
      this.imgData.imgArr = imgArr;
      this.imgData.imgIndex = index || 0;
    },
    // 型号／衍生型号搜索
    seach(index) {
      if (index === 0) {
        this.pageData.params.page = 1;
        this.pageData2.list = [];
        this.pageData2.params.parentId = null;
        this.pageData2.params.page = 1;
        this.getList();
      } else {
        if( this.pageData2.params.parentId == 0 ) {
        this.pageData2.params.parentId = null;
        }
        this.pageData2.params.page = 1;
        this.getList(this.pageData2);
      }
    },
    // 选择型号查询衍生型号
    choose(value) {
      this.getList(this.pageData2);
    },
    // 获取型号数据
    getList(list) {
      this.listLoading = true;
      list = list || this.pageData;
      archivesApi.page(list.params).then(response => {
        this.$set(list, "list", response.data.rows);
        this.$set(list, "total", response.data.total);
        this.listLoading = false;
      });
    },
    // 型号数据Size变更
    handleSizeChange(val) {
      this.pageData.params.size = val;
      this.pageData.params.page = 1;
      this.getList();
    },
    // 衍生型号数据Size变更
    handleSizeChange2(val) {
      this.pageData2.params.size = val;
      this.pageData2.params.page = 1;
      this.getList(this.pageData2);
    },
    // 型号数据Page变更
    handlePageChange(val) {
      this.pageData.params.page = val;
      this.getList();
    },
    // 衍生型号数据Page变更
    handlePageChange2(val) {
      this.pageData.params.page = val;
      this.getList(this.pageData2);
    },
    // 变更按钮页面跳转
    handleUpdate(type, modelPbsId) {
      if (type == "trans") {
        this.$router.push({
          path: "/product/archives/model/editTrans",
          query: {
            modelPbsId
          }
        });
      } else {
        this.$router.push({
          path: "/product/archives/model/edit",
          query: {
            modelPbsId
          }
        });
      }
    },
    // 变更
    handleChange (modelId) {
        const params = {
          id: modelId
        };
        archivesApi.updateAuth(params).then(response => {
          console.log(response);
          if (response.code === 200) {
            this.$message({
              type: "success",
              message: "发起成功!"
            });
            this.getList()
          }
        });  
    },
    //导出模版
    exportExcel() {
      this.ipVisible = true;
    },
    exportSubmit() {
      const excleObj = {};
      if (this.pdsFileModelBaseDto) {
        excleObj.categoryId = this.pdsFileModelBaseDto.pdsConfCategory[
          this.pdsFileModelBaseDto.pdsConfCategory.length - 1
        ];
        excleObj.standard = this.pdsFileModelBaseDto.standard;
        window.location.href =
          process.env.BASE_API +
          "/api/pds/pdsFileExcel/downloadTemplate?categoryId=" +
          excleObj.categoryId +
          "&standard=" +
          excleObj.standard;
        this.ipVisible = false;
      }
    }
  }
};
</script>
